package com.cqyc.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.cqyc.shop.comm.JdbcUtils;
import com.cqyc.shop.comm.commException;
import com.cqyc.shop.domain.Orders;

public class OrdersDao {
	
	
	public  Orders  rowmap(ResultSet rs) throws SQLException  {
		 Orders orders = new Orders();
		 orders.setId(rs.getInt("id"));
		 orders.setUsercode(rs.getString("usercode"));
		 orders.setShopname(rs.getString("shopname"));
		 orders.setOrdercode(rs.getString("ordercode"));
		 orders.setOrderimg(rs.getString("orderimg"));
		 orders.setShoptime(rs.getString("shoptime"));
		 orders.setCartinfo(rs.getString("cartinfo"));
		 orders.setPrice(rs.getBigDecimal("price"));
		 orders.setHowmany(rs.getInt("howmany"));
		 orders.setBuytotal(rs.getInt("buytotal"));
		 return orders;
	}
	/**
	 * 保存数据到订单表中
	 * @param orders
	 */
	public void save(Orders orders) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet  rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="insert into orders(usercode,shopname,ordercode,orderimg,shoptime,cartinfo,price,howmany,buytotal)  values(?,?,?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, orders.getUsercode());
			ps.setString(2, orders.getShopname());
			ps.setString(3, orders.getOrdercode());
			ps.setString(4, orders.getOrderimg());
			ps.setString(5, orders.getShoptime());
			ps.setString(6,orders.getCartinfo());
			ps.setBigDecimal(7, orders.getPrice());
			ps.setInt(8, orders.getHowmany());
			ps.setInt(9, orders.getBuytotal());
			int ret = ps.executeUpdate();
			if(ret < 0) {
				throw new commException("数据插入失败");
			}
		} catch (Exception e) {
				throw new commException("数据插入失败2");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}

	/**
	 * 从数据库中查询出
	 * @param orders
	 * @return
	 */
	public List<Orders> findPager(Orders orders) {
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet  rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select  *  from orders where usercode=? and shopname=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, orders.getUsercode());
			ps.setString(2, orders.getShopname());
			rs = ps.executeQuery();
			List<Orders> orders2 = new ArrayList<>();
			while (rs.next()) {
				orders2.add(rowmap(rs));
			}
			return orders2;
		} catch (Exception e) {
			throw new commException("查询数据失败");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
	
	
	public List<Orders> findPager(String  usercode) {
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet  rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select  *  from orders where usercode=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, usercode);
			rs = ps.executeQuery();
			List<Orders> orders2 = new ArrayList<>();
			while (rs.next()) {
				orders2.add(rowmap(rs));
			}
			return orders2;
		} catch (Exception e) {
			throw new commException("查询数据失败");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
	/**
	 *  将数据从订单表中删除掉
	 * @param idd
	 */
	public void delete(Integer idd) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet  rs = null;
		
		try {
			conn = JdbcUtils.getConnerction();
			String sql="delete from orders where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, idd);
			int ret = ps.executeUpdate();
			if(ret<0) {
				throw new commException("删除数据失败");
			}
		} catch (Exception e) {
			throw new commException("删除数据异常");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		
		
	}

}
